package com.tiger.cloud.service.crm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.dao.crm.CommucationDao;
import com.tiger.cloud.dao.crm.CustomerDao;
import com.tiger.cloud.dao.crm.PeersDao;
import com.tiger.cloud.enums.CommucationCustomerType;
import com.tiger.cloud.enums.YesOrNoEnum;
import com.tiger.cloud.model.crm.Commucation;
import com.tiger.cloud.model.crm.SystemCommucation;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;

/**
 * 交流记录
 */
@Service("commucationService")
public class CommucationService extends TigerBaseService<Commucation> implements ICommucationService{
    
    @Autowired
	private CommucationDao commucationDao;
    
    @Autowired
    private CustomerDao customerDao;
    
    @Autowired 
    private PeersDao peersDao;

	@Override
	public MybatisTigerDao<Commucation> getEntityDao() {
		return commucationDao;
	}
    
	public Commucation getCommucation(Commucation commucation){
		return commucationDao.getCommucation(commucation);
	}

	@Override
	public Page<Commucation> findByGroupCustomer(Commucation commucation) {
		int limitStart=commucation.getLimitStart(),limitEnd = commucation.getLimitEnd();
		long count = commucationDao.countByGroupCustomer(commucation);
		Page<Commucation> page = new Page<Commucation>((int)(limitEnd/(limitEnd-limitStart)), (limitEnd-limitStart));
		page.setData(commucationDao.findByGroupCustomer(commucation));
		page.setTotalCount(count);
		return page;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean insertSystemCommucation(List<SystemCommucation> systemCommucations) {
		List<Commucation> list=new ArrayList<Commucation>();
		systemCommucations.forEach(systemCommucation->{
			if(StringUtils.isEmpty(systemCommucation.getContent())){
				throw new ServiceException(ErrorCode.INSERT_DATA_ERROR);
			}
			Commucation commucation=new Commucation();
			commucation.setCustomerId(systemCommucation.getCustomerId());
			commucation.setCustomerType(systemCommucation.getCustomerType());
			commucation.setCommucationDate(new Date());
			commucation.setIsSystemMsg(YesOrNoEnum.YES);
			commucation.setContent(systemCommucation.getContent());
			commucation.setUserId(systemCommucation.getUserId());
			list.add(commucation);
			updateLastCommunicateTime(commucation);
		});
		if(commucationDao.insertBatch(list)>0){
			return true;
		}
		throw new ServiceException(ErrorCode.INSERT_COMMUCATION_ERROR);
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean insertSystemCommucation(SystemCommucation systemCommucation) {
		if(StringUtils.isEmpty(systemCommucation.getContent())){
			throw new ServiceException("Content不能为空");
		}
		Commucation commucation=new Commucation();
		commucation.setCommucationDate(new Date());
		commucation.setIsSystemMsg(YesOrNoEnum.YES);
		commucation.setCustomerId(systemCommucation.getCustomerId());
		commucation.setCustomerType(systemCommucation.getCustomerType());
		commucation.setContent(systemCommucation.getContent());
		commucation.setUserId(systemCommucation.getUserId());
		if( commucationDao.insert(commucation)>0){
			return updateLastCommunicateTime(commucation);
		};
		throw new ServiceException(ErrorCode.INSERT_COMMUCATION_ERROR);
	}

	@Override
	public boolean insertAndUpdateCommunicatTime(Commucation commucation) {
		if(commucationDao.insert(commucation)>0){
			return updateLastCommunicateTime(commucation);
		}
		throw new ServiceException(ErrorCode.INSERT_COMMUCATION_ERROR);
	}
	
	@Override
	public boolean updateByUserId(Commucation commucation) {
		if(commucationDao.updateByUserId(commucation)>0){
			return true;
		}
		throw new ServiceException(ErrorCode.UPDATE_COMMUCATION_ERROR);
	}
	@Override
	public boolean updateLastCommunicateTime(Commucation commucation) {
			if(commucation.getCustomerType() == CommucationCustomerType.CUSTOMER){
				try {
					 if(customerDao.updateLastCommunicateTime(commucation.getCustomerId())>0){
						 return true;
					 }else{
						 throw new ServiceException(ErrorCode.UPDATE_LAST_COMMUNICATE_TIME_ERROR);
					 }
				} catch (Exception e) {
					e.printStackTrace();
					throw new ServiceException(ErrorCode.UPDATE_LAST_COMMUNICATE_TIME_ERROR);
				}
			}else if(commucation.getCustomerType() == CommucationCustomerType.PEERS){
				try {
					 if(peersDao.updateLastCommunicateTime(commucation.getUserId(),commucation.getCustomerId())>0){
						 return true;
					 }else{
						 throw new ServiceException(ErrorCode.UPDATE_LAST_COMMUNICATE_TIME_ERROR);
					 }
				} catch (Exception e) {
					e.printStackTrace();
					throw new ServiceException(ErrorCode.UPDATE_LAST_COMMUNICATE_TIME_ERROR);
				}
			}else{
				throw new ServiceException(ErrorCode.CUSTOMER_TYPE_ERROR);
			}
	}
	
}